/**
 * $Id: MobileShoppingListApplication.m 29 2008-01-17 07:10:07Z robertsaenz $
 */
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UIView.h>
#import <UIKit/UINavigationItem.h>
#import <UIKit/UIAlertSheet.h>
#import "MobileShoppingListApplication.h"
#import "ShoppingListView.h"


static MobileShoppingListApplication *sharedInstance;

@implementation MobileShoppingListApplication

+ (MobileShoppingListApplication *)sharedInstance {
	if (!sharedInstance)
		sharedInstance = [[MobileShoppingListApplication alloc] init];
	return sharedInstance;
}

- (void) applicationWillTerminate {
	NSLog ( @"MobileShoppingList will terminate?" );
}

- (void) applicationResume: (struct __GSEvent *) event {
	NSLog ( @"MobileShoppingList - resume" );
}

- (void) applicationSuspend: (struct __GSEvent *) event {
	NSLog ( @"MobileShoppingList - suspend" );
	[self terminateWithSuccess];

}

- (void)applicationWillSuspend {
	NSLog ( @"MobileShoppingList will suspend?" );
}

- (void) applicationDidFinishLaunching: (id) unused {
	// Initialize singleton
	sharedInstance = self;
	editMode = NO;
	// Configuration paths
	dirPath = @"/var/root/Library/MobileShoppingList";
	filePath = @"/var/root/Library/MobileShoppingList/ShoppingList.db";
	preferenceFilePath = @"/var/root/Library/MobileShoppingList/preferences.dat";
	// Check to see if directory exists
	if (![[NSFileManager defaultManager] fileExistsAtPath:dirPath])	{
		[[NSFileManager defaultManager] createDirectoryAtPath:dirPath attributes:nil];
	}
	// UI Setup
	fullScreenRect = [UIHardware fullScreenApplicationContentRect];
	mainWindow = [[UIWindow alloc] initWithContentRect: fullScreenRect];
	fullScreenRect.origin.x = 0.0f;
	fullScreenRect.origin.y = 0.0f;
	mainView = [[UIView alloc] initWithFrame: fullScreenRect];
	// Nav bar top
	navBarTop = [[UINavigationBar alloc] initWithFrame: CGRectMake(fullScreenRect.origin.x, fullScreenRect.origin.y, fullScreenRect.size.width, 45.0f)];
	[navBarTop setDelegate:self];
	// Transition view
	transitionView = [[UITransitionView alloc] initWithFrame:CGRectMake(fullScreenRect.origin.x,
						45.0f, fullScreenRect.size.width, fullScreenRect.size.height - 45.0f)];
	// Add views to main view
	[mainView addSubview:navBarTop];
	[mainView addSubview:transitionView];
	[mainView addSubview:navBarBot];
	mainWindow = [[UIWindow alloc] initWithContentRect:fullScreenRect];
	[mainWindow setContentView:mainView];
	[mainWindow orderFront:self];
	[mainWindow makeKey:self];
	if(!shopListView) {
		shopListView = [[ShoppingListView alloc] initWithFrame: CGRectMake(0.0f, 0.0f,
							fullScreenRect.size.width, fullScreenRect.size.height - 45.0f)];
	}
	[self loadView: shopListView withTrans: 1 andPushItem: YES];
}


- (void) addShoppingListItem: (NSString *) itemName {
	[shopListView addItem: itemName];
}

- (void) loadView: (id) view withTrans: (int) trans andPushItem: (BOOL) pushItem {
	switch([view getViewType]) {
		case SHOPLIST:
			if(pushItem){
				[navBarTop showButtonsWithLeftTitle: @"Edit" rightTitle: @" + " leftBack: NO];
				UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: [view getViewTitle]];
				[navBarTop pushNavigationItem: navItem];
			} else {
				if(curViewType == SHOPLIST) {
					if([view getEditMode]){
						[navBarTop showLeftButton:@"Done" withStyle:3 rightButton:@"Clear" withStyle:1];
					} else {
						[navBarTop showButtonsWithLeftTitle: @"Edit" rightTitle: @" + " leftBack: NO];
					}
				} else {
					[view reloadData];
					[navBarTop showButtonsWithLeftTitle: @"Edit" rightTitle: @" + " leftBack: NO];
					[navBarTop popNavigationItem];
				}
			}
			break;
		case CATLIST:
			if(pushItem){
				UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: [view getViewTitle]];
				[navBarTop showButtonsWithLeftTitle: [shopListView getViewTitle] rightTitle: @"Test" leftBack: YES];
				[navBarTop pushNavigationItem: navItem];
			} else {
				[navBarTop popNavigationItem];
				[navBarTop showButtonsWithLeftTitle: [shopListView getViewTitle] rightTitle: @"Test" leftBack: YES];
			}
			break;
		case ITEMLIST:
			if(pushItem){
				UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: [view getViewTitle]];
				[navBarTop showButtonsWithLeftTitle: [catListView getViewTitle] rightTitle: NO leftBack: YES];
				[navBarTop pushNavigationItem: navItem];
			} else {
				[navBarTop popNavigationItem];
			}
			break;
		default:
			break;
	}
	curViewType = [view getViewType];
	[transitionView transition:trans toView: view];
}

- (void) removeShoppingItem: (Item *) item {
	[shopListView removeItem: item];
}

// Delegate functions
- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button {
	switch (curViewType) {
		case SHOPLIST:
			if (button == 0){
				if([shopListView getEditMode]) {
					[self showConfirmation: @"Are you sure you want to clear your shopping list?" withTitle: @"Clear List" andConfirmType: CLEARLIST];
				} else {
					if(!catListView){
						catListView = [[CategoryListView alloc] initWithFrame: CGRectMake(0.0f, 0.0f,
									fullScreenRect.size.width, fullScreenRect.size.height - 45.0f)];
					}
					[self loadView: catListView withTrans: 1 andPushItem: YES];
				}
			} else {
				[shopListView toggleEditMode];
				[self loadView: shopListView withTrans:1 andPushItem: NO];
			}
			break;
		case CATLIST:
			if(button == 0) {
				// Code added for testing purposes
				[shopListView addItem: @"New Item!"];
				//TODO: Do we handle this?
			} else {
				[shopListView reloadData];
				[self loadView: shopListView withTrans: 2 andPushItem: NO];
			}
			break;
		case ITEMLIST:
			if(button == 1){
				[self loadView: catListView withTrans: 2 andPushItem: NO];
			}
			break;
		default:
			break;
	}
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
	if(whichConfirm == DELITEM){
		if(button == 1){
			[shopListView deleteSelectedItem];
		}
		// TODO: Handle delete item
	} else if (whichConfirm == CLEARLIST) {
	  if(button == 1){
		  [shopListView clearTable];
	  }
	}
  [sheet dismiss];
}

// Dialog functions
- (void) showConfirmation: (NSString *) message withTitle: (NSString *) title andConfirmType: (confirmType) type {
	// Allert sheet displayed at centre of screen.
	NSArray *buttons = [NSArray arrayWithObjects:@"Yes", @"No", nil];
	UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithTitle:title buttons:buttons defaultButtonIndex:1 delegate:self context:self];
	[alertSheet setBodyText:message];
	[alertSheet popupAlertAnimated:YES];
	whichConfirm = type;
}


@end
